package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.PublicGroupDao;
import com.xbongbong.saas.domain.entity.PublicGroupEntity;
import com.xbongbong.saas.model.PublicGroupModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 公海池分组
 * @Author: hongxiao
 * @date: 2019-03-03 17:33
 * @since v1.0
 */
@Service("publicGroupModel")
public class PublicGroupModelImpl implements PublicGroupModel {

    @Resource
    private PublicGroupDao publicGroupDao;

    @Override
    public Integer insert(PublicGroupEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return publicGroupDao.insert(entity);
    }

    @Override
    public Integer update(PublicGroupEntity entity) throws XbbException {
        entity.setUpdateTime((long)DateUtil.getInt());
        return publicGroupDao.update(entity);
    }

    @Override
    public Integer save(PublicGroupEntity entity) throws XbbException {
        if(entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public PublicGroupEntity getByKey( Long key, String corpid) { return publicGroupDao.getByKey(key, corpid); }

    @Override
    public Integer deleteByKey( Long key, String corpid) { return publicGroupDao.deleteByKey(key, corpid); }

    @Override
    public List<PublicGroupEntity> findEntitys(Map<String, Object> param) {
        return publicGroupDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return publicGroupDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer getCorpsCount() { return publicGroupDao.getCorpsCount(); }

    @Override
    public List<String> findCorpIds(Map<String ,Object> param) { return publicGroupDao.findCorpIds(param); }

    @Override
    public List<PublicGroupEntity> enableListByFormId(String corpid, Long formId) {
//        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
//        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
//        param.put(StringConstant.CORPID, corpid);
//        param.put("orderByStr", "sort desc");
//        param.put("enable", BasicConstant.IS_USE);
//        param.put("formId", formId);
        return enableListByFormIdList(corpid,Collections.singletonList(formId));
    }

    @Override
    public List<PublicGroupEntity> enableListByFormIdList(String corpid, List<Long> formIdList) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(StringConstant.DEL, DelEnum.NORMAL.getDel());
        param.put(StringConstant.CORPID, corpid);
        param.put("orderByStr", "sort desc");
        param.put("enable", BasicConstant.IS_USE);
        param.put("formIdIn", formIdList);
        return findEntitys(param);
    }
}
